<template>
  <div>
    <div class="filter-container _formStyle_" style="padding: 10px 0">
      <el-form ref="form" :inline="true" :model="form" label-width="80px">
        <el-form-item label="数据库表:">
          <myElAutocomplete
            :form="form"
            attributeName="_tableName_"
            :seleteLists="importanceOptions"
            @inputEvent="handleBASICstrparam"
            valuekey="Label_Desc"
            placeholder="数据库表"
          />
        </el-form-item>
        <el-form-item label="组件名称:">
          <el-input
            v-model.trim="form.componentName"
            placeholder="组件名称"
            style="width: 160px"
            clearable
          />
        </el-form-item>
        <el-form-item label="操作类型:">
          <el-select v-model="form.operateType" placeholder clearable>
            <el-option
              v-for="item in typeLists"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-form-item>
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="handleFilter"
              >查询</el-button
            >
            <el-button
              class="filter-item"
              style="margin-left: 10px"
              type="primary"
              icon="el-icon-edit-outline"
              @click="handleReset"
              >重置</el-button
            >
          </el-form-item>
        </el-form-item>
      </el-form>
    </div>
    <el-table height="690" :data="list" highlight-current-row>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="组件id"
      />
      <el-table-column
        prop="componentName"
        header-align="center"
        align="center"
        label="组件名称"
      />
      <el-table-column
        prop="operateType"
        header-align="center"
        align="center"
        label="操作类型"
      >
        <template slot-scope="scope">
          {{
            scope.row.operateType == "SELECT"
              ? "查询"
              : scope.row.operateType == "UPDATE"
              ? "更新"
              : scope.row.operateType == "DELETE"
              ? "删除"
              : scope.row.operateType == "SQL"
              ? "SQL"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column
        prop="paramsIn"
        header-align="center"
        align="center"
        label="接收参数"
      >
        <template slot-scope="scope">
          <div
            v-for="(item, index) in JSON.parse(scope.row.paramsIn)"
            :key="index"
          >
            {{ item.desc }}-{{ item.name }}-{{ item.type }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="returnType"
        header-align="center"
        align="center"
        label="返回结果类型"
      />
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="deleteHandle(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="form.page"
      :limit.sync="form.limit"
      @pagination="getList"
    />
    <el-dialog
      :title="editTitle"
      :visible.sync="dialogVisible"
      width="67%"
      :close-on-click-modal="false"
      v-if="dialogVisible"
    >
      <keep-alive>
        <component
          :is="currentComponent"
          :editDate="editDate"
          @close="handleClose"
        ></component>
      </keep-alive>
    </el-dialog>
  </div>
</template>

<script>
import {
  projectdatebaseLists,
  projectTableLists,
  projectcustomdelete,
} from "@/api/lowcode";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination 分页组件
import customQuery from "./customQuery.vue";
import customDeletion from "./customDeletion.vue";
import customUpdates from "./customUpdates.vue";
import customApiSearch from "./customApiSearch.vue";
import myElAutocomplete from "../commonComponents/myElAutocomplete.vue";
export default {
  name: "customComponents",
  components: {
    Pagination,
    customQuery,
    customDeletion,
    customUpdates,
    customApiSearch,
    myElAutocomplete,
  },
  data() {
    return {
      currentComponent: "customQuery",
      editDate: {},
      dialogVisible: false,
      editTitle: "查询组件编辑",
      // 筛选框的 form 表单
      form: {
        _tableName_: "",
        tableName: "",
        componentName: "",
        operateType: "",
        // page: 1,
        // limit: 20
      },
      list: [], // 表格数据
      total: 0, // 总条数
      importanceOptions: [],
      typeLists: [
        {
          code: "SELECT",
          name: "查询",
        },
        {
          code: "UPDATE",
          name: "更新",
        },
        {
          code: "DELETE",
          name: "删除",
        },
        {
          code: "SQL",
          name: "SQL",
        },
      ],
    };
  },
  // async created() {
  //   this.getList();
  //   this.projectdatebaseLists();
  // },
  //由于该组件添加了keep-alive缓存，所以每次进入这个组件调用activated生命周期
  activated() {
    this.getList();
    this.projectdatebaseLists();
  },
  methods: {
    handleBASICstrparam(val) {
      let obj = this.importanceOptions.find((item) => item.Label_Desc === val);
      if (obj) {
        this.form.tableName = obj.className;
      } else {
        this.form.tableName = "";
      }
    },
    handleClose(flag) {
      this.dialogVisible = false;
      if (flag) {
        this.getList();
      }
    },
    async projectdatebaseLists() {
      this.importanceOptions = [];
      const res = await projectdatebaseLists(
        +sessionStorage.getItem("developLowCodeId")
      );
      if (res?.code === 200) {
        this.importanceOptions = res.data;
        this.importanceOptions.forEach((item) => {
          this.$set(
            item,
            "Label_Desc",
            item.classDesc
              ? `${item.className} | ${item.classDesc}`
              : item.className
          );
        });
      }
    },
    // 获取表格数据
    async getList() {
      this.list = [];
      // this.total = 0;
      const res = await projectTableLists({
        ...this.form,
        projectId: +sessionStorage.getItem("developLowCodeId"),
      });
      if (res?.code === 200) {
        this.list = res.data;
        // this.total = res.data.totalCount;
      }
    },
    // 重新获取表格数据 页码归为1
    handleFilter() {
      // this.form.page = 1;
      this.getList();
    },
    handleReset() {
      this.form = {
        _tableName_: "",
        tableName: "",
        componentName: "",
        operateType: "",
      };
      this.getList();
    },
    handleUpdate(row) {
      this.dialogVisible = true;
      this.editDate = JSON.parse(JSON.stringify(row));
      this.$set(this.editDate, "inParams", JSON.parse(row.paramsIn));
      this.$set(this.editDate, "tableName", row.operateTable);
      if (row.operateType === "SELECT") {
        this.editTitle = "查询组件编辑";
        this.currentComponent = "customQuery";
        this.$set(this.editDate, "condition", JSON.parse(row.componentParams));
      } else if (row.operateType === "UPDATE") {
        this.editTitle = "更新组件编辑";
        this.currentComponent = "customUpdates";
        const componentParams = JSON.parse(row.componentParams);
        this.$set(this.editDate, "condition", componentParams.updateCondition);
        this.$set(
          this.editDate,
          "updateColumnValue",
          componentParams.updateColumnValue
        );
      } else if (row.operateType === "DELETE") {
        this.editTitle = "删除组件编辑";
        this.currentComponent = "customDeletion";
        this.$set(this.editDate, "condition", JSON.parse(row.componentParams));
      } else if (row.operateType === "SQL") {
        this.editTitle = "SQL组件编辑";
        this.currentComponent = "customApiSearch";
        this.$set(this.editDate, "sql", row.componentParams);
        const index1 = this.editDate.returnType.indexOf("<");
        const index2 = this.editDate.returnType.lastIndexOf(">");
        if (index1 >= 0 && index2 >= 0) {
          this.$set(
            this.editDate,
            "elementType",
            this.editDate.returnType.slice(index1 + 1, index2)
          );
          this.$set(
            this.editDate,
            "returnType",
            this.editDate.returnType.slice(0, index1)
          );
        }
      }
    },
    async deleteHandle(item) {
      this.$confirm(`确定要进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const res = await projectcustomdelete([item.id]);
        if (res?.code === 200) {
          this.$message({
            //message: res.message,
            message: "删除成功",
            type: "success",
          });
          this.getList();
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/styles/layout-table.scss";
$rowLength: 2;
@media all and (min-width: 1659px) {
  $rowLength: 2;
  @include calculation($rowLength: $rowLength);
}
@media all and (max-width: 1659px) {
  $rowLength: 3;
  @include calculation($rowLength: $rowLength);
}
._formStyle_ ::v-deep .el-form-item__label {
  text-align: left;
  margin-top: 4px;
}
</style>

